package com.codechuan.demo.ui.my;

import static android.os.Looper.getMainLooper;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.codechuan.demo.MainActivity;
import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.databinding.FragmentMyBinding;
import com.codechuan.demo.ui.myMainToken.MyMainToken;
import com.codechuan.demo.ui.myToken.MyToken;
import com.codechuan.demo.utils.DialogUtil;


public class MyFragment extends Fragment implements View.OnClickListener {
    private Handler handler;
    private FragmentMyBinding binding;
    private int loginOutNumber = 0;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        MyViewModel myViewModel =
                new ViewModelProvider(this).get(MyViewModel.class);
        binding = FragmentMyBinding.inflate(inflater, container, false);
        // 小号Token
        Button jumpToken = binding.jumpToken;
        jumpToken.setOnClickListener(this);
        // 主号Token
        Button jumpMainToken = binding.jumpMainToken;
        jumpMainToken.setOnClickListener(this);
        // 退出登录
        Button sy = binding.sy;
        sy.setOnClickListener(this);
        // 解绑卡密
        Button jumpBind = binding.jumpBind;
        jumpBind.setOnClickListener(this);
        // 获取主线程
        handler = new Handler(getMainLooper());
        // 主号状态
        final TextView textView = binding.mainAccountStatus;
        myViewModel.getText(Constant.MAIN_ACCOUNT_STATUS).observe(getViewLifecycleOwner(), textView::setText);
        // 到期时间
        binding.dqTime.setText(Constant.DQ_TIME);

        final TextView textAccountToken = binding.mainAccount;
        if (Constant.MAIN_TOKEN == null || Constant.MAIN_TOKEN.isEmpty()) {
            textAccountToken.setText("重新打开软件获取主号或设置主号.");
            myViewModel.getText("未查询到主号").observe(getViewLifecycleOwner(), textView::setText);
        } else {
            String tokenString = " Token:" + Constant.MAIN_TOKEN;
            textAccountToken.setText(tokenString);
        }
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (binding.jumpToken.getId() == id) {
            Intent intent = new Intent();
            intent.setClass(Constant.context, MyToken.class);
            startActivity(intent);
        }
        if (R.id.jumpMainToken == id) {
            // 判断是否在抢单中
            if (Constant.isStart) {
                DialogUtil.showDialog(Constant.context, "当前抢单中,无法管理主号");
                return;
            }
            Intent intent = new Intent();
            intent.setClass(Constant.context, MyMainToken.class);
            startActivity(intent);
        }
        if (R.id.sy == id) {
            loginOutNumber = loginOutNumber + 1;
           if (loginOutNumber < 2){
               DialogUtil.showDialog(Constant.context, "再点击一次退出登录.");
           }else {
               Constant.isStart = false;
               Intent intent = new Intent();
               intent.setClass(Constant.context, MainActivity.class);
               startActivity(intent);
           }
        }
        if (R.id.jumpBind == id){
            handler.post(() -> DialogUtil.showDialog(Constant.context, "请联系客户解绑."));
        }
    }
}
